package com.niuwa.p2p.action.messagemanager;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.springframework.beans.factory.annotation.Autowired;

import com.itextpdf.text.pdf.PdfStructTreeController.returnType;
import com.niuwa.p2p.action.JsonAction;
import com.niuwa.p2p.action.loanclaim.model.LoanClaimWithProdModel;
import com.niuwa.p2p.core.mvc.entity.JsonResult;
import com.niuwa.p2p.core.mvc.entity.PageBean;
import com.niuwa.p2p.core.utils.StringUtil;
import com.niuwa.p2p.entity.PageQuery;
import com.niuwa.p2p.entity.message.Message;
import com.niuwa.p2p.service.message.IMessageService;
import com.niuwa.p2p.service.message.ISendMsgService;
import com.niuwa.p2p.vo.claim.LoanClaimVo;
import com.opensymphony.xwork2.ActionContext;

/**
 * @author futianliang
 * @date 2015年4月14日
 */
public class MessageAction extends JsonAction {
	private static final long serialVersionUID = -8900124777631535966L;
	/** 日志 */
	private static Logger logger = Logger.getLogger(MessageAction.class);
	@Autowired
	private IMessageService messageService;
	@Autowired
	private ISendMsgService sendMsgService;

	/* *
	 * 跳转到发送信息列表
	 */
	@Action(value = "gotoSendMessageManage")
	public String gotoLoanClaimListManagePage() {
		return "sendMessageManage";
	}

	private Long id;

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	/**
	 * 查询发送信息列表
	 * 
	 * @return
	 * @throws Exception
	 */
	@Action(value = "sendMessageList")
	public String sendMessageList() throws Exception {
		setObjectReSult(queryPage());
		logger.info("sendMessageList action execute success");
		return NONE;
	}

	@Action("gotoMessageContent")
	public String gotoClaimDetails() throws Exception {
		ActionContext context = ActionContext.getContext();
		Message message = messageService.getMessageById(id);
		context.put("message", message.getContent());
		int row = 4;
		int row1 = message.getContent().length() / 20 + 3;
		if (row1 > 8) {
			row = 8;
		} else {
			row = row1 < 4 ? 4 : row1;
		}
		context.put("height", String.valueOf(110 - row * 10));
		context.put("row", String.valueOf(row));
		return "messageContent";
	}

	private PageBean<Message> queryPage() {

		// 获取查询参数
		Map<String, Object> paramMap = getParamMap();
		if (paramMap.get("messageCondition") != null) {
			paramMap.put("messageCondition", String.valueOf(paramMap.get("messageCondition")).trim().replaceAll("%", "\\\\%").replaceAll("_", "\\\\_"));
		}
		if (paramMap.get("sendEndTime") != null && !StringUtil.isEmpty(String.valueOf(paramMap.get("sendEndTime")))) {
			paramMap.put("sendEndTime", String.valueOf(paramMap.get("sendEndTime")).trim() + " 23:59:59");
		}

		int totalCount = messageService.getMessageListCount(paramMap);
		PageBean<Message> page = new PageBean<Message>(totalCount);
		// 当前页
		page.setPage(((PageQuery) paramMap.get(PageQuery.PAGEQUERY)).getPage());
		page.setRows(messageService.getMessageList(paramMap));
		return page;
	}

	/**
	 * 重新发送消息
	 */
	@SuppressWarnings("unchecked")
	@Action("anewSendMessage")
	public String anewSendMessage() {
		boolean isSuccess = false;
		JsonResult results = new JsonResult();
		try {
			isSuccess = sendMsgService.reSendMsg(id);
			results.setMsg(String.valueOf(isSuccess));
		} catch (Exception e) {
			results.setMsg("发送失败");
			e.printStackTrace();
		} finally {
			setResult(results);
		}
		return NONE;
	}
	@Action("getMessageById")
	public String getMessageById() {
		Message mess = null;
		JsonResult results = new JsonResult();
		try {
			mess = messageService.getMessageById(id);
			results.setData(mess);
		} catch (Exception e) {
			results.setMsg("发送失败");
			e.printStackTrace();
		} finally {
			setResult(results);
		}
		return NONE;
	}
}
